package com.example.tvrecyclerview.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * @author: njb
 * @date: 2020/7/4 0004 0:25
 * @desc: 自定义FocusLinearLayoutManager
 */
public class FocusLinearLayoutManager extends LinearLayoutManager {


    public FocusLinearLayoutManager(Context context) {
        super(context);
    }

    public FocusLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public FocusLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,
                                    int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public View onInterceptFocusSearch(View focused, int direction) {

        int currentPosition = getPosition(getFocusedChild());//这里要用这个方法
        int count = getItemCount();
        int lastVisiblePosition = findLastVisibleItemPosition();
        switch (direction) {
            case View.FOCUS_RIGHT:
                currentPosition++;
                break;
            case View.FOCUS_LEFT:
                currentPosition--;
                break;
        }
        if (currentPosition < 0 || currentPosition > count) {
            return focused;
        } else {
            if (currentPosition > lastVisiblePosition){
                scrollToPosition(currentPosition);
            }
        }
        return super.onInterceptFocusSearch(focused, direction);
    }
}